package com.example.qxapp.Fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.qxapp.Bean.Post;
import com.example.qxapp.Bean.User;
import com.example.qxapp.R;
import com.example.qxapp.activity.MypushActivity;
import com.example.qxapp.adapter.Fmypushadapter;
import com.example.qxapp.adapter.mypushadapter;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class Framentpush extends Fragment {
    private SwipeRefreshLayout swipe_mypush2;
    private RecyclerView rv_mypush2;

    private TextView error_mypush2;
    List<Post> data;
    private Fmypushadapter fmypushadapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        refresh();
        swipe_mypush2.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    private void refresh() {
        User user = BmobUser.getCurrentUser(User.class);
        BmobQuery<Post> bmobQuery = new BmobQuery<>();
        bmobQuery.addWhereEqualTo("author",user);
        bmobQuery.order("-createdAt");
        bmobQuery.findObjects(new FindListener<Post>() {
            @Override
            public void done(List<Post> list, BmobException e) {
                swipe_mypush2.setRefreshing(false);
                if (e==null){
                    data = list;
                    if (data.size()>0){
                        swipe_mypush2.setVisibility(View.VISIBLE);

                        fmypushadapter = new Fmypushadapter(getActivity(),data);
                        rv_mypush2.setLayoutManager(new LinearLayoutManager(getActivity()));
                        rv_mypush2.setAdapter(fmypushadapter);

                    }else {
                        error_mypush2.setVisibility(View.VISIBLE);
                    }
                }
//                else {
//                    Toast.makeText(MypushActivity.this, "加载失败"+e, Toast.LENGTH_SHORT).show();
//                }
            }
        });
    }

    private void init() {
        swipe_mypush2=getActivity().findViewById(R.id.swipe_mypush2);
        rv_mypush2=getActivity().findViewById(R.id.rv_mypush2);
        error_mypush2=getActivity().findViewById(R.id.error_mypush2);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mypush, container,false);
    }
}
